// Copyright © SixtyFPS GmbH <info@slint.dev>
// SPDX-License-Identifier: GPL-3.0-only OR LicenseRef-Slint-Royalty-free-2.0 OR LicenseRef-Slint-Software-3.0

import { FluentFontSettings, FluentPalette } from "styling.slint";
import { ScrollView } from "scrollview.slint";
import { TextEditBase } from "../common/textedit-base.slint";

export component TextEdit {
    in property <TextWrap> wrap <=> base.wrap;
    in property <TextHorizontalAlignment> horizontal-alignment <=> base.horizontal-alignment;
    in property <bool> read-only <=> base.read-only;
    in property <length> font-size <=> base.font-size;
    in property <bool> enabled <=> base.enabled;
    in property <string> placeholder-text <=> base.placeholder-text;
    in-out property <bool> has-focus: base.has-focus;
    out property <length> visible-width <=> base.visible-width;
    out property <length> visible-height <=> base.visible-height;
    in-out property <string> text <=> base.text;
    in-out property <length> viewport-x <=> base.viewport-x;
    in-out property <length> viewport-y <=> base.viewport-y;
    in-out property <length> viewport-width <=> base.viewport-width;
    in-out property <length> viewport-height <=> base.viewport-height;

    callback edited <=> base.edited;
    callback key-pressed <=> base.key-pressed;
    callback key-released <=> base.key-released;

    accessible-role: AccessibleRole.text-input;
    accessible-enabled: root.enabled;
    accessible-value <=> text;
    accessible-placeholder-text: text == "" ? placeholder-text : "";
    accessible-read-only: root.read-only;

    public function set-selection-offsets(start: int, end: int) {
        base.set-selection-offsets(start, end);
    }

    public function select-all() {
        base.select-all();
    }

    public function clear-selection() {
        base.clear-selection();
    }

    public function cut() {
        base.cut();
    }

    public function copy() {
        base.copy();
    }

    public function paste() {
        base.paste();
    }

    forward-focus: base;
    horizontal-stretch: 1;
    vertical-stretch: 1;

    states [
        disabled when !root.enabled: {
            base.background: FluentPalette.control-disabled;
            base.border-color: FluentPalette.border;
            base.foreground: FluentPalette.text-disabled;
            base.selection-foreground-color: FluentPalette.text-accent-foreground-disabled;
        }
        focused when root.has-focus: {
            base.background: FluentPalette.control-input-active;
            base.border-color: FluentPalette.border;
            i-focus-border.background: FluentPalette.accent-background;
        }
    ]

    base := TextEditBase {
        width: 100%;
        height: 100%;
        background: FluentPalette.control-background;
        border-radius: 4px;
        border-width: 1px;
        border-color: FluentPalette.text-control-border;
        scroll-view-padding: 12px;
        foreground: FluentPalette.foreground;
        font-size: FluentFontSettings.body.font-size;
        font-weight: FluentFontSettings.body.font-weight;
        placeholder-color: FluentPalette.text-secondary;
        selection-background-color: FluentPalette.selection-background;
        selection-foreground-color: FluentPalette.selection-foreground;

        i-focus-border := Rectangle {
            x: parent.border-radius;
            y: parent.height - self.height;
            width: parent.width - 2 * parent.border-radius;
            height: 2px;
        }
    }
}
